package com.dmz.workflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmz.workflow.base.BaseServiceImpl;
import com.dmz.workflow.constant.CommonConstants;
import com.dmz.workflow.constant.ExceptionMessageConstants;
import com.dmz.workflow.dao.FormGroupMapper;
import com.dmz.workflow.service.FormGroupService;
import com.dmz.workflow.util.ObjectUtils;
import com.dmz.workflow.vo.FormGroupVo;
import com.echronos.commons.Result;
import com.echronos.commons.enums.ErrorMsgEnum;
import com.echronos.commons.model.RequestUser;
import com.echronos.commons.req.BasePageReq;
import com.echronos.commons.utils.CopyObjectUtils;
import com.echronos.commons.utils.RequestUserUtils;
import com.dmz.workflow.entity.FormGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FormGroupServiceImpl extends BaseServiceImpl<FormGroupMapper, FormGroup> implements FormGroupService {

    @Autowired
    private FormGroupMapper formGroupMapper;

    /**
     * 查询组
     **/
    @Override
    public IPage<FormGroup> queryListById(FormGroupVo echGroupFormVO) {
        RequestUser user = RequestUserUtils.getUser();
        FormGroup echGroupForm = CopyObjectUtils.copyAtoB(echGroupFormVO, FormGroup.class);
        echGroupForm.setCompanyId(user.getCompanyId());
        BasePageReq basePageReq = new BasePageReq();
        Page<FormGroup> page = new Page<>(basePageReq.getPageNo(), 100);
        return formGroupMapper.queryListById(page, echGroupForm);
    }

    /**
     * 修改组 需要id数组 和 sort数组 名称数组
     **/
    @Override
    public void update(List<FormGroupVo> vchGroupFormVO) {
        RequestUser user = RequestUserUtils.getUser();
        vchGroupFormVO.forEach(formGroupVO -> {
            formGroupVO.setUpdateTime(LocalDateTime.now());
            formGroupVO.setUpdateUser(user.getMemberId());
            formGroupVO.setCompanyId(user.getCompanyId());
        });
        formGroupMapper.updateGroupById(vchGroupFormVO);
    }

    /**
     * 更具id删除分组
     **/
    @Override
    public void deleteGroup(Integer id) {
        RequestUser user = RequestUserUtils.getUser();
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("updateTime", LocalDateTime.now());
        map.put("updateUser", user.getMemberId());
        formGroupMapper.deleteGroupById(map);
    }

    /**
     * 新增分组
     **/
    @Override
    public Result<?> addFormGroup(FormGroupVo echGroupFormVO) {
        FormGroup echGroupForm = CopyObjectUtils.copyAtoB(echGroupFormVO, FormGroup.class);
        echGroupForm.setAppId(1);
        echGroupForm.setLang("cn");
        echGroupForm.setTenantId(1);
        RequestUser user = RequestUserUtils.getUser();
        Integer companyId = user.getCompanyId();
        echGroupForm.setCompanyId(companyId);
        echGroupForm.setCreateUser(user.getMemberId());
        echGroupForm.setCreateTime(LocalDateTime.now());
        if (queryCurrentCompanyValidGroup(companyId, echGroupFormVO.getName()) > 0) {
            return Result.build(ErrorMsgEnum.NORMAL_ERR.getErrorCode(), ExceptionMessageConstants.GROUP_NAME_DUPLICATE_MESSAGE);
        }
        Integer integer = formGroupMapper.selectCount(new LambdaQueryWrapper<FormGroup>().eq(FormGroup::getCompanyId, companyId));
        if (ObjectUtils.isNotEmpty(integer)) {
            echGroupForm.setOrd(++integer);
        } else {
            echGroupForm.setOrd(0);
        }
        formGroupMapper.insert(echGroupForm);
        return Result.build();
    }

    @Override
    public Result<?> updateGroupName(FormGroupVo echGroupFormVO) {
        RequestUser user = RequestUserUtils.getUser();
        Integer companyId = user.getCompanyId();
        if (queryCurrentCompanyValidGroup(companyId, echGroupFormVO.getName()) > 0) {
            return Result.build(ErrorMsgEnum.NORMAL_ERR.getErrorCode(), ExceptionMessageConstants.GROUP_NAME_DUPLICATE_MESSAGE);
        }
        FormGroup formGroup = new FormGroup();
        formGroup.setId(echGroupFormVO.getId());
        formGroup.setName(echGroupFormVO.getName());
        formGroupMapper.updateById(formGroup);
        return Result.build();
    }

    /**
     * 回显数据接口
     **/
    @Override
    public FormGroupVo queryGroup(Integer id) {
        FormGroup formGroup = formGroupMapper.selectById(id);
        return CopyObjectUtils.copyAtoB(formGroup, FormGroupVo.class);
    }

    @Override
    public int queryCurrentCompanyValidGroup(Integer companyId, String groupName) {
        LambdaQueryWrapper<FormGroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FormGroup::getName, groupName)
                .eq(FormGroup::getCompanyId, companyId)
                .ne(FormGroup::getIsDeleted, CommonConstants.DATA_DELETED_STATUS);
        return formGroupMapper.selectCount(lambdaQueryWrapper);
    }


}
